import time
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore

from data_process import data_to_plot
from data_process import update_data_to_plot

# 导入命令行参数
from parse_definition import args

row = 0
curve = 0
file_name = 0

# 数据更新，后台任务


def data_update():
    # 填充数据
    global row
    global curve
    global file_name

    update_data_to_plot(file_name, 2, row)
    row += 1

    # 更新图形数据并绘制
    curve.setData(data_to_plot)  # 以numpy矩阵为数据更新绘图

# 应用创建


def app_create(log_file_path):
    global curve
    global file_name
    file_name = log_file_path

    # 创建应用程序
    app = pg.mkQApp("Real Time Plot")

    # 创建窗口并命名
    win = pg.GraphicsLayoutWidget(show=True, title="Real Time Plot")
    win.resize(800, 600)
    win.setWindowTitle('Real Time Plot')

    # 声明图形绘制
    plot_1 = win.addPlot()
    # plot_1.setAutoPan(y=True) # 设置自动缩放y坐标
    # plot_1.enableAutoRange('y', 0.95)

    # 声明曲线的绘制
    curve = plot_1.plot()  # 绘制图形，稍候输入参数

    # 启用QtCore里的定时器QTimer
    # 绑定应用程序
    # 配置周期启动时间
    timer = QtCore.QTimer()
    timer.timeout.connect(data_update)

    if (args.update_freq):
        update_period = 1000/args.update_freq
    else:
        update_period = 50
    timer.start(update_period)  # ms

    pg.exec()


if __name__ == '__main__':
    app_create(args.log_path + args.log_name)
